package com.rcache.core.utils;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;



/**
 * @desc   邮件发送的工具类
 * @author 高增源
 * @time   2022-01-27
 */
public class EmailUtils {
    //发送者自己的邮箱
    private static String sendEmailAccount="1007226575@qq.com";
    //邮箱授权码
    private static String sendEmailPwd="ajieqyaiqqeybcdb";
    //收件人邮箱
//    public  static String receiveMailAccount="980472475@qq.com";


    /**
     * @desc  创建并发送用户反馈文本邮件
     * @param title 标题
     * @param receiveMailContent 正文
     * @throws UnsupportedEncodingException
     * @throws MessagingException
     */
    public static void createMineMessage(String title,String receiveMailContent,String receiveMailAccount) throws UnsupportedEncodingException, MessagingException
    {
        Session session = EmailUtils.getSession();
        //3.创建一封邮件(可以简单的理解为是那封信)
        MimeMessage  message=new MimeMessage(session);
        //4.From:发件人
        message.setFrom(new InternetAddress(sendEmailAccount, "Rcache预警系统", "utf-8"));

        // 5. To: 收件人（可以增加多个收件人、抄送、密送）
        //    CC:抄送人，BCC:密送
        message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(receiveMailAccount, "尊敬的管理员", "utf-8"));

        // 6. Subject: 邮件主题（标题有广告嫌疑，避免被邮件服务器误认为是滥发广告以至返回失败，请修改标题）
        message.setSubject("Rcache预警信息", "utf-8");

        // 7. Content: 邮件正文（可以使用html标签)
        String context = feedbackModule(title,receiveMailContent);
        message.setContent(context, "text/html;charset=utf-8");
        saveAndSendEmail(message, session);
    }

    /**
     * @desc 反馈信息邮件模板
     * @param content
     * @return
     */
    private static String feedbackModule(String title,String content){
        String str=
                "<style>\n" +
                        "\t\t\t.qmbox .email-body {\n" +
                        "\t\t\t\tcolor: #40485B;\n" +
                        "\t\t\t\tfont-size: 14px;\n" +
                        "\t\t\t\tfont-family: -apple-system, 'Helvetica Neue', Helvetica, 'Nimbus Sans L', 'Segoe UI', Arial, 'Liberation Sans', 'PingFang SC', 'Microsoft YaHei', 'Hiragino Sans GB', 'Wenquanyi Micro Hei', 'WenQuanYi Zen Hei', 'ST Heiti', SimHei, 'WenQuanYi Zen Hei Sharp', sans-serif;\n" +
                        "\t\t\t\tbackground: #f8f8f8;\n" +
                        "\t\t\t}\n" +
                        "\t\t\n" +
                        "\t\t\t.qmbox .pull-right {\n" +
                        "\t\t\t\tfloat: right;\n" +
                        "\t\t\t}\n" +
                        "\t\t\n" +
                        "\t\t\t.qmbox a {\n" +
                        "\t\t\t\tcolor: #FE7300;\n" +
                        "\t\t\t\ttext-decoration: underline;\n" +
                        "\t\t\t}\n" +
                        "\t\t\n" +
                        "\t\t\t.qmbox a:hover {\n" +
                        "\t\t\t\tcolor: #fe9d4c;\n" +
                        "\t\t\t}\n" +
                        "\t\t\n" +
                        "\t\t\t.qmbox a:active {\n" +
                        "\t\t\t\tcolor: #b15000;\n" +
                        "\t\t\t}\n" +
                        "\t\t\n" +
                        "\t\t\t.qmbox .logo {\n" +
                        "\t\t\t\ttext-align: center;\n" +
                        "\t\t\t\tmargin-bottom: 20px;\n" +
                        "\t\t\t}\n" +
                        "\t\t\n" +
                        "\t\t\t.qmbox .panel {\n" +
                        "\t\t\t\tbackground: #fff;\n" +
                        "\t\t\t\tborder: 1px solid #E3E9ED;\n" +
                        "\t\t\t\tmargin-bottom: 10px;\n" +
                        "\t\t\t}\n" +
                        "\t\t\n" +
                        "\t\t\t.qmbox .panel-header {\n" +
                        "\t\t\t\tfont-size: 18px;\n" +
                        "\t\t\t\tline-height: 30px;\n" +
                        "\t\t\t\tpadding: 10px 20px;\n" +
                        "\t\t\t\tbackground: #fcfcfc;\n" +
                        "\t\t\t\tborder-bottom: 1px solid #E3E9ED;\n" +
                        "\t\t\t}\n" +
                        "\t\t\n" +
                        "\t\t\t.qmbox .panel-body {\n" +
                        "\t\t\t\tpadding: 20px;\n" +
                        "\t\t\t\tline-height: 1.7;\n" +
                        "\t\t\t}\n" +
                        "\t\t\n" +
                        "\t\t\t.qmbox .container {\n" +
                        "\t\t\t\twidth: 100%;\n" +
                        "\t\t\t\tmax-width: 600px;\n" +
                        "\t\t\t\tpadding: 20px;\n" +
                        "\t\t\t\tmargin: 0 auto;\n" +
                        "\t\t\t}\n" +
                        "\t\t\n" +
                        "\t\t\t.qmbox .text-center {\n" +
                        "\t\t\t\ttext-align: center;\n" +
                        "\t\t\t}\n" +
                        "\t\t\n" +
                        "\t\t\t.qmbox .thumbnail {\n" +
                        "\t\t\t\tpadding: 4px;\n" +
                        "\t\t\t\tmax-width: 100%;\n" +
                        "\t\t\t\tborder: 1px solid #E3E9ED;\n" +
                        "\t\t\t}\n" +
                        "\t\t\n" +
                        "\t\t\t.qmbox .btn-primary {\n" +
                        "\t\t\t\tcolor: #fff;\n" +
                        "\t\t\t\tfont-size: 16px;\n" +
                        "\t\t\t\tpadding: 8px 14px;\n" +
                        "\t\t\t\tline-height: 20px;\n" +
                        "\t\t\t\tborder-radius: 2px;\n" +
                        "\t\t\t\tdisplay: inline-block;\n" +
                        "\t\t\t\tbackground: #FE7300;\n" +
                        "\t\t\t\ttext-decoration: none;\n" +
                        "\t\t\t}\n" +
                        "\t\t\n" +
                        "\t\t\t.qmbox .btn-primary:hover,\n" +
                        "\t\t\t.qmbox .btn-primary:active {\n" +
                        "\t\t\t\tcolor: #fff;\n" +
                        "\t\t\t}\n" +
                        "\t\t\n" +
                        "\t\t\t.qmbox .footer {\n" +
                        "\t\t\t\tcolor: #9B9B9B;\n" +
                        "\t\t\t\tfont-size: 12px;\n" +
                        "\t\t\t\tmargin-top: 20px;\n" +
                        "\t\t\t}\n" +
                        "\t\t\n" +
                        "\t\t\t.qmbox .footer a {\n" +
                        "\t\t\t\tcolor: #9B9B9B;\n" +
                        "\t\t\t}\n" +
                        "\t\t\n" +
                        "\t\t\t.qmbox .footer a:hover {\n" +
                        "\t\t\t\tcolor: #fe9d4c;\n" +
                        "\t\t\t}\n" +
                        "\t\t\n" +
                        "\t\t\t.qmbox .footer a:active {\n" +
                        "\t\t\t\tcolor: #b15000;\n" +
                        "\t\t\t}\n" +
                        "\t\t\n" +
                        "\t\t\t.qmbox .email-body#mail_to_teacher {\n" +
                        "\t\t\t\tline-height: 26px;\n" +
                        "\t\t\t\tcolor: #40485B;\n" +
                        "\t\t\t\tfont-size: 16px;\n" +
                        "\t\t\t\tpadding: 0px;\n" +
                        "\t\t\t}\n" +
                        "\t\t\n" +
                        "\t\t\t.qmbox .email-body#mail_to_teacher .container,\n" +
                        "\t\t\t.qmbox .email-body#mail_to_teacher .panel-body {\n" +
                        "\t\t\t\tpadding: 0px;\n" +
                        "\t\t\t}\n" +
                        "\t\t\n" +
                        "\t\t\t.qmbox .email-body#mail_to_teacher .container {\n" +
                        "\t\t\t\tpadding-top: 20px;\n" +
                        "\t\t\t}\n" +
                        "\t\t\n" +
                        "\t\t\t.qmbox .email-body#mail_to_teacher .textarea {\n" +
                        "\t\t\t\tpadding: 32px;\n" +
                        "\t\t\t}\n" +
                        "\t\t\n" +
                        "\t\t\t.qmbox .email-body#mail_to_teacher .say-hi {\n" +
                        "\t\t\t\tfont-weight: 500;\n" +
                        "\t\t\t}\n" +
                        "\t\t\n" +
                        "\t\t\t.qmbox .email-body#mail_to_teacher .paragraph {\n" +
                        "\t\t\t\tmargin-top: 24px;\n" +
                        "\t\t\t}\n" +
                        "\t\t\n" +
                        "\t\t\t.qmbox .email-body#mail_to_teacher .paragraph .pro-name {\n" +
                        "\t\t\t\tcolor: #000000;\n" +
                        "\t\t\t}\n" +
                        "\t\t\n" +
                        "\t\t\t.qmbox .email-body#mail_to_teacher .paragraph.link {\n" +
                        "\t\t\t\tmargin-top: 32px;\n" +
                        "\t\t\t\ttext-align: center;\n" +
                        "\t\t\t}\n" +
                        "\t\t\n" +
                        "\t\t\t.qmbox .email-body#mail_to_teacher .paragraph.link .button {\n" +
                        "\t\t\t\tbackground: #4A90E2;\n" +
                        "\t\t\t\tborder-radius: 2px;\n" +
                        "\t\t\t\tcolor: #FFFFFF;\n" +
                        "\t\t\t\ttext-decoration: none;\n" +
                        "\t\t\t\tpadding: 11px 17px;\n" +
                        "\t\t\t\tline-height: 14px;\n" +
                        "\t\t\t\tdisplay: inline-block;\n" +
                        "\t\t\t}\n" +
                        "\t\t\n" +
                        "\t\t\t.qmbox .email-body#mail_to_teacher ul.pro-desc {\n" +
                        "\t\t\t\tlist-style-type: none;\n" +
                        "\t\t\t\tmargin: 0px;\n" +
                        "\t\t\t\tpadding: 0px;\n" +
                        "\t\t\t\tpadding-left: 16px;\n" +
                        "\t\t\t}\n" +
                        "\t\t\n" +
                        "\t\t\t.qmbox .email-body#mail_to_teacher ul.pro-desc li {\n" +
                        "\t\t\t\tposition: relative;\n" +
                        "\t\t\t}\n" +
                        "\t\t\n" +
                        "\t\t\t.qmbox .email-body#mail_to_teacher ul.pro-desc li::before {\n" +
                        "\t\t\t\tcontent: '';\n" +
                        "\t\t\t\twidth: 3px;\n" +
                        "\t\t\t\theight: 3px;\n" +
                        "\t\t\t\tborder-radius: 50%;\n" +
                        "\t\t\t\tbackground: red;\n" +
                        "\t\t\t\tposition: absolute;\n" +
                        "\t\t\t\tleft: -15px;\n" +
                        "\t\t\t\ttop: 11px;\n" +
                        "\t\t\t\tbackground: #40485B;\n" +
                        "\t\t\t}\n" +
                        "\t\t\n" +
                        "\t\t\t.qmbox .email-body#mail_to_teacher .blackboard-area {\n" +
                        "\t\t\t\theight: 600px;\n" +
                        "\t\t\t\tpadding: 40px;\n" +
                        "\t\t\t\tbackground-image: url(https://gitee.com/wewin11235/upload-gitee-image/raw/master/bg.jpg);\n" +
                        "\t\t\t\tcolor: #FFFFFF;\n" +
                        "\t\t\t}\n" +
                        "\t\t\n" +
                        "\t\t\t.qmbox .email-body#mail_to_teacher .blackboard-area .big-title {\n" +
                        "\t\t\t\tfont-size: 32px;\n" +
                        "\t\t\t\tline-height: 45px;\n" +
                        "\t\t\t\ttext-align: center;\n" +
                        "\t\t\t}\n" +
                        "\t\t\n" +
                        "\t\t\t.qmbox .email-body#mail_to_teacher .blackboard-area .desc {\n" +
                        "\t\t\t\tmargin-top: 8px;\n" +
                        "\t\t\t}\n" +
                        "\t\t\n" +
                        "\t\t\t.qmbox .email-body#mail_to_teacher .blackboard-area .desc p {\n" +
                        "\t\t\t\tmargin: 0px;\n" +
                        "\t\t\t\ttext-align: center;\n" +
                        "\t\t\t\tline-height: 28px;\n" +
                        "\t\t\t}\n" +
                        "\t\t\n" +
                        "\t\t\t.qmbox .email-body#mail_to_teacher .blackboard-area .card:nth-child(odd) {\n" +
                        "\t\t\t\tfloat: left;\n" +
                        "\t\t\t\tmargin-top: 45px;\n" +
                        "\t\t\t}\n" +
                        "\t\t\n" +
                        "\t\t\t.qmbox .email-body#mail_to_teacher .blackboard-area .card:nth-child(even) {\n" +
                        "\t\t\t\tfloat: right;\n" +
                        "\t\t\t\tmargin-top: 45px;\n" +
                        "\t\t\t}\n" +
                        "\t\t\n" +
                        "\t\t\t.qmbox .email-body#mail_to_teacher .blackboard-area .card .title {\n" +
                        "\t\t\t\tfont-size: 18px;\n" +
                        "\t\t\t\ttext-align: center;\n" +
                        "\t\t\t\tmargin-bottom: 10px;\n" +
                        "\t\t\t}\n" +
                        "\t\t</style>\n" +
                        "\t\t<div id='mailContentContainer' class='qmbox qm_con_body_content qqmail_webmail_only' style='opacity: 1;'>\n" +
                        "\t\t\t<div class='email-body'>\n" +
                        "\t\t\t\t<div class='container'>\n" +
                        "\t\t\t\t\t<div class='logo'>\n" +
                        "\t\t\t\t\t\tRcache预警\n" +
                        "\t\t\t\t\t</div>\n" +
                        "\t\t\t\t\t<div class='panel'>\n" +
                        "\t\t\t\t\t\t<div class='panel-header'>\n" +
                        "\t\t\t\t\t\t\t预警通知\n" +
                        "\t\t\t\t\t\t</div>\n" +
                        "\t\t\t\t\t\t<div class='panel-body'>\n" +
                        "\t\t\t\t\t\t\t"+
                        "<h1>"+title+"</h1>"+
                        "<p style='font-size: 20px;'>"+content+"</p>"
                        +"\n" +
                        "\t\t\t\t\t\t\t\n" +
                        "\t\t\t\t\t\t</div>\n" +
                        "\t\t\t\t\t</div>\n" +
                        "\n" +
                        "\t\t\t\t</div>\n" +
                        "\t\t\t</div>\n" +
                        "\n" +
                        "\t\t\t<table cellpadding='0' cellspacing='0' border='0' width='100%'>\n" +
                        "\t\t\t\t<tbody>\n" +
                        "\t\t\t\t\t<tr>\n" +
                        "\t\t\t\t\t\t<td align='center' style='overflow: hidden;font-size: 0;padding: 0;margin: 0;line-height: 0;'>\n" +
                        "\t\t\t\t\t\t\t<img style='width:0;height:0'\n" +
                        "\t\t\t\t\t\t\t\tsrc='https://sctrack.sendcloud.net/track/open/eyJtYWlsbGlzdF9pZCI6IDAsICJ0YXNrX2lkIjogIiIsICJlbWFpbF9pZCI6ICIxNjQyMTU4OTU1MTk5XzQyMTI5XzI0MDI1XzU4MzMuc2MtMTBfOV8xM18yMTMtaW5ib3VuZDAkOTgwNDcyNDc1QHFxLmNvbSIsICJzaWduIjogImM2OTBjMDM3NDVjYTk2MWZlZTYyMDJmMDA2OGY3YmUzIiwgInVzZXJfaGVhZGVycyI6IHt9LCAibGFiZWwiOiAwLCAidHJhY2tfZG9tYWluIjogInNjdHJhY2suc2VuZGNsb3VkLm5ldCIsICJyZWFsX3R5cGUiOiAiIiwgIm5ldGVhc2UiOiAiZmFsc2UiLCAib3V0X2lwIjogIjExNy41MC42Mi44MyIsICJjb250ZW50X3R5cGUiOiAiMCIsICJ1c2VyX2lkIjogNDIxMjksICJvdmVyc2VhcyI6ICJmYWxzZSIsICJjYXRlZ29yeV9pZCI6IDEyMjI2OH0=.gif'>\n" +
                        "\t\t\t\t\t\t</td>\n" +
                        "\t\t\t\t\t</tr>\n" +
                        "\t\t\t\t</tbody>\n" +
                        "\t\t\t</table>\n" +
                        "\t\t\t\n" +
                        "\t\t\t<style type='text/css'>\n" +
                        "\t\t\t\t.qmbox style,\n" +
                        "\t\t\t\t.qmbox script,\n" +
                        "\t\t\t\t.qmbox head,\n" +
                        "\t\t\t\t.qmbox link,\n" +
                        "\t\t\t\t.qmbox meta {\n" +
                        "\t\t\t\t\tdisplay: none !important;\n" +
                        "\t\t\t\t}\n" +
                        "\t\t\t</style>\n" +
                        "\t\t</div>";
        return str;
    }

    /**
     * @desc  2.保存和发送
     * @param message
     * @param session
     * @throws MessagingException
     */
    private static void saveAndSendEmail(MimeMessage message,Session session) throws MessagingException
    {
        // 8. 设置发送时间
        message.setSentDate(new Date());

        // 9. 保存设置
        message.saveChanges();

        // 10. 根据 Session 获取邮件传输对象
        Transport transport = session.getTransport();

        //11.连接SMTP服务器
        transport.connect(sendEmailAccount, sendEmailPwd);

        // 12. 发送邮件, 发到所有的收件地址, message.getAllRecipients() 获取到的是在创建邮件对象时添加的所有收件人, 抄送人, 密送人
        transport.sendMessage(message, message.getAllRecipients());

        // 13. 关闭连接
        transport.close();

    }



    /**
     * @desc  将发送邮件的前3步抽取成一个函数，目的是方便后续的代码的复用
     * @return
     */
    private static Session getSession()
    {
        //1.创建参数配置, 用于连接邮件服务器的参数配置
        Properties props=new Properties();
        props.setProperty("mail.transport.protocol","smtp");// 使用的协议（JavaMail规范要求）
        props.setProperty("mail.smtp.host","smtp.qq.com"); // 发件人的邮箱的 SMTP 服务器地址
        props.setProperty("mail.smtp.auth", "true");    // 需要请求认证

        // 2. 根据配置创建会话对象, 用于和邮件服务器交互
        Session session=Session.getInstance(props);
        session.setDebug(true);//以debug的模式进行运行，方便后续出错误时候的调试
        return session;
    }

}
